//保存采样计划主表
function saveForm(){
		var plan={};
		plan.id=$("#id").val();
		plan.planDate=$("#planDate").val();
		$.ajax({
			  type: "Post",
			  url: $.suredy.contextPath +"/plan/saveForm",
			  data:JSON.stringify(plan),
			  cache: false,
			  async:false,
			  contentType : 'application/json;charset=utf-8',
			  success: function(obj){
				  $.notify('保存成功！');
					 },
			  error:function(XMLHttpRequest, textStatus, errorThrown){
				  $.notify('保存失败！<br/>'+errorThrown);
			  }
			});
}
//选择采样点
function selPlace(){
	$.suredy.showModal({
		title : "选取采样点",
		uri :$.suredy.contextPath +'/plan/selplace',
		okClick:function(){
			if($('.nodeRow').length>0){
				var items = new Array();
				$('.nodeRow').each(function(index){
					items.push({planInfoId:$("#id").val(),placeName:$(this).attr("data-text"),placeId:$(this).attr("data-id")});
				});
				items = savaItem(items);
				$.each(items, function(i,item){      
					$("#palceList").append('<tr><td align="center"><input type="checkbox" name="places" value="'+item.id+'"></td><td align="center">' + item.placeName + '</td><td align="center"></td><td align="center"></td></tr>');
				  });
				$.suredy.closeModal();
			}else{
				alert('请选采样点！');
			}
		}
	});
}

//选择采样员
function selGrabber(){
	var ids=0;
	$("input:checkbox[name='places']:checked").each(function(){ 
			ids++;
	}); 
	if(ids==0){
		alert('请勾选采样点，再设置人员！');
		return;
	}
	$.suredy.showModal({
		title : "设置采样员",
		uri : $.suredy.contextPath +'/plan/selgrabber',
		okClick:function(){
			var selUser= $('input:radio[name="user"]:checked');
			var items = new Array();
			if(selUser.val()!=null){
				$("input:checkbox[name='places']:checked").each(function(){ 
					items.push({id:$(this).val(),grabberName:selUser.attr("data-username"),grabberId:selUser.val()});
					$(this).parent().parent().children().eq(2).html(selUser.attr("data-username"));
				}); 
				savaItem(items);
				$("input:checkbox").prop("checked",false);
				$.suredy.closeModal();
			}else{
				alert('请选采样员！');
			}
		}
	});
}
//选择实验类别
function selGrabType(){
	var ids=0;
	$("input:checkbox[name='places']:checked").each(function(){ 
			ids++;
	}); 
	if(ids==0){
		alert('请勾选采样点，再设置实验类型！');
		return;
	}
	$.suredy.showModal({
		title : "设置采样员",
		uri : $.suredy.contextPath +'/plan/seltesttype',
		okClick:function(){
			var selType= $('input:radio[name="testtype"]:checked');
			var items = new Array();
			if(selType.val()!=null){
				$("input:checkbox[name='places']:checked").each(function(){ 
					items.push({id:$(this).val(),testTypeName:selType.attr("data-type-name"),testTypeId:selType.val()});
					$(this).parent().parent().children().eq(3).html(selType.attr("data-type-name"));
				}); 
				savaItem(items);
				$("input:checkbox").prop("checked",false);
				$.suredy.closeModal();
			}else{
				alert('请选采样员！');
			}
		}
	});
}
//保存采样点及相关数据
function savaItem(items){
	var _temp;
	$.ajax({
		  type: "Post",
		  url: $.suredy.contextPath +"/plan/saveItem",
		  data:JSON.stringify(items),
		  cache: false,
		  contentType : 'application/json;charset=utf-8',
		  dataType: "json",
		  async:false,
		  success: function(obj){
			  _temp=obj;
			  },
		  error:function(XMLHttpRequest, textStatus, errorThrown){
			  alert(XMLHttpRequest );
		  }
		});
	return _temp;
}
//删除采样点
function delPlace(){
	var items=new Array();
	$("input:checkbox[name='places']:checked").each(function(){ 
		items.push({id:$(this).val()});
	}); 
	if(items.length==0){
		alert('请勾选需要删除的采样点！');
		return;
	}
	$.ajax({
		  type: "Post",
		  url: $.suredy.contextPath +"/plan/delitem",
		  data:JSON.stringify(items),
		  cache: false,
		  contentType : 'application/json;charset=utf-8',
		  async:false,
		  success: function(obj){
			  $("input:checkbox[name='places']:checked").each(function(){ 
					$(this).parent().parent().remove();
				}); 	
			  alert('删除成功！');
			  	
				 },
		  error:function(XMLHttpRequest, textStatus, errorThrown){
			  alert(XMLHttpRequest );
		  }
		});
}
//全选采样点
function setSelAll(obj){
	if($(obj).prop("checked")){
		$("input:checkbox[name='places']").prop("checked",true);
	}else{
		$("input:checkbox[name='places']").prop("checked",false);
	}
}
//全选实验项
function setSelAll2(obj){
	if($(obj).prop("checked")){
		$("input:checkbox[name='testItem']").prop("checked",true);
	}else{
		$("input:checkbox[name='testItem']").prop("checked",false);
	}
}

//设置检测员对话框
function setInspector(){
	var ids=0;
	$("input:checkbox[name='testItem']:checked").each(function(){ 
			ids++;
	}); 
	if(ids==0){
		alert('请勾选实验类型，再设置人员！');
		return;
	}
	$.suredy.showModal({
		title : "设置采样员",
		uri : $.suredy.contextPath +'/plan/setinspector',
		okClick:function(){
			var selUser= $('input:radio[name="user"]:checked');
			var items = new Array();
			if(selUser.val()!=null){
				$("input:checkbox[name='testItem']:checked").each(function(){ 
					items.push({id:$(this).val(),inspectorName:selUser.attr("data-username"),inspectorId:selUser.val()});
					$(this).parent().parent().children().eq(2).html(selUser.attr("data-username"));
				}); 
				saveInspector(items);
				$("input:checkbox").prop("checked",false);
				$.suredy.closeModal();
			}else{
				alert('请选采样员！');
			}
		}
	});
}

//设置检测员后保存数据
function saveInspector(items){
	var retVal=false;
	$.ajax({
		  type: "Post",
		  url: $.suredy.contextPath +"/plan/updateinspector",
		  data:JSON.stringify(items),
		  cache: false,
		  contentType : 'application/json;charset=utf-8',
		  async:false,
		  success: function(obj){
			  retVal= true;
				 },
		  error:function(XMLHttpRequest, textStatus, errorThrown){
			  retVal= false;
		  }
		});
	return retVal;
}

//根据实验类型创建实验项目
function createTestItem(){	
	var retVal=false;
	$.ajax({
		  type: "Post",
		  url: $.suredy.contextPath +"/plan/createitem",
		  data:{planId:$("#id").val()},
		  cache: false,
		  async:false,
		  success: function(obj){
			  retVal= true;
				 },
		  error:function(XMLHttpRequest, textStatus, errorThrown){
			  retVal= false;
		  }
		});
	return retVal;
}

//完成采样计划的制定，生成采样编码
function finishPlace(){
	var retVal=false;
	if($("input:checkbox[name='places']").length>0){
		var isAllSet=true;
		$("input:checkbox[name='places']").each(function(){
			if($(this).parent().parent().children().eq(2).html()==""||$(this).parent().parent().children().eq(3).html()==""){
				isAllSet=false;
				return ;
			}
		});
		if(!isAllSet){
			alert("请设置采样点的实验类型及采样员！");
			return retVal;
		}
	}else{
		alert("请设置采样点！");
		return retVal;
	}
	$.ajax({
		  type: "Post",
		  url: $.suredy.contextPath +"/plan/finishplace",
		  data:{planId:$("#id").val()},
		  cache: false,
		  async:false,
		  success: function(obj){
			  retVal= true;
				 },
		  error:function(XMLHttpRequest, textStatus, errorThrown){
			  retVal= false;
		  }
		});
	return retVal;
}

//完成采样，生成检测项
function finishGrab(){
	var retVal=false;
	$.ajax({
		  type: "Post",
		  url: $.suredy.contextPath +"/plan/finishGrab",
		  data:{planId:$("#id").val()},
		  cache: false,
		  async:false,
		  success: function(obj){
			  retVal=true;
				 },
		  error:function(XMLHttpRequest, textStatus, errorThrown){
			  retVal=false;
		  }
		});
	return retVal;
}
//生成检测任务
function createTest(){
	var retVal=false;
	if($("input:checkbox[name='testItem']").length>0){
		var isAllSet=true;
		$("input:checkbox[name='testItem']").each(function(){
			if($(this).parent().parent().children().eq(2).html()==""){
				isAllSet=false;
				return ;
			}
		});
		if(!isAllSet){
			alert("请设置检测人员！");
			return retVal;
		}
	}else{
		alert("数据设置错误，未生成实验类型！");
		return retVal;
	}
	$.ajax({
		  type: "Post",
		  url: $.suredy.contextPath +"/plan/createTest",
		  data:{planId:$("#id").val()},
		  cache: false,
		  async:false,
		  success: function(obj){
			  retVal= true;
				 },
		  error:function(XMLHttpRequest, textStatus, errorThrown){
			  retVal= false;
		  }
		});
	return retVal;
}

//生产检测汇总表
function createRep(){
	var retVal=false;
	$.ajax({
		  type: "Post",
		  url: $.suredy.contextPath +"/plan/createRep",
		  data:{planId:$("#id").val()},
		  cache: false,
		  async:false,
		  success: function(obj){
				  if(obj.success)
					  retVal = true;
				  else{
					  alert(obj.msg);
					  retVal= false;
				  }
				},
		  error:function(XMLHttpRequest, textStatus, errorThrown){
			  retVal= false;
		  }
		});
	return retVal;
}
//打印采样任务单
function printJob(){
	alert('模板需确认......');
}


//打印采样编号标签
function printTab(){
	$.suredy.showModal({
		title : "打印标签",
		uri : $.suredy.contextPath +'/plan/printTab',
		data:{id:$("#id").val()},
		okClick:function(){
			$.suredy.closeModal();
		}
	});
}